<script setup lang="ts">
defineOptions({
  name: 'RegisterIndex'
})

import router from '@/router'
import { RulesCode, RulesMobile, RulesPassword } from '@/utils/rules'
import { ref } from 'vue'
// 定义响应式数据
const mobile = ref('')
const code = ref('')
const password = ref('')

// 导包
import { useMobileCode } from '@/composables/useMobileCode'
import { showFailToast, showToast } from 'vant'
import { registerAPI } from '@/apis/user'
// 调用函数得到函数的返回值 返回值是一个对象 将这个对象进行解构
const { onSend, time, formRef } = useMobileCode(mobile, 'register')
const isArgee = ref(false)
// 注册
const onSubmit = async () => {
  if (!isArgee.value) return showFailToast('请勾选我同意')
  // 发送请求
  await registerAPI(mobile.value, code.value, password.value)
  showToast('注册成功')
  // 跳转到登录页面
  router.push('/login')
}

// 准备响应式的数据
const isShow = ref(false)
</script>

<template>
  <div class="register-page">
    <cp-nav-bar
      right-text="登录"
      @click-right="router.push('/login')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="register-head">
      <h3>注册</h3>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" ref="formRef" @submit="onSubmit">
      <van-field
        placeholder="请输入手机号"
        type="tel"
        v-model="mobile"
        name="mobile"
        :rules="RulesMobile"
      ></van-field>
      <van-field
        placeholder="请输入验证码"
        v-model="code"
        name="code"
        :rules="RulesCode"
      >
        <template #button>
          <span
            class="btn-send"
            @click="onSend"
            :class="{ active: time > 0 }"
            >{{ time > 0 ? `${time}秒后发送验证码` : '发送验证码' }}</span
          >
        </template>
      </van-field>
      <van-field
        placeholder="请输入密码"
        :type="`${isShow ? 'text' : 'password'}`"
        v-model="password"
        name="password"
        :rules="RulesPassword"
      >
        <template #button>
          <cp-icon
            @click="isShow = !isShow"
            :name="`${isShow ? 'login-eye-on' : 'login-eye-off'}`"
            class="icon-eye"
          ></cp-icon>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isArgee">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">
          注 册
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.register {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .icon-eye {
    margin-right: 10px;
  }
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
